var xmlhttp;

function getXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return xmlhttp;
}

function addAnswer(questionId)
{
	getXmlHttpObject();
	var nickname = document.getElementById("nickname").value;
	var introduce = document.getElementById("introduce").value;
	var content = document.getElementById("content").value;
	
	var url = "../controllers/addAnswerController.php";
	var params = "nickname=" + nickname + "&introduce=" + introduce + "&content=" + content + "&questionId=" + questionId; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = addAnswerSuccess;
	xmlhttp.send(params);
}

function addAnswerSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			window.location.href="../views/questionlist.php?page=1"; 
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteQuestion(questionId)
{
	getXmlHttpObject();
	var url = "../controllers/deleteQuestionController.php";
	var params = "questionId=" + questionId; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = deleteQuestionSuccess;
	xmlhttp.send(params);
}

function deleteQuestionSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			window.location.reload(); 
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteAnswer(answerId)
{
	getXmlHttpObject();
	var url = "../controllers/deleteAnswerController.php";
	var params = "answerId=" + answerId; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = deleteAnswerSuccess;
	xmlhttp.send(params);
}

function deleteAnswerSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			window.location.reload(); 
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

